from news_data import SinaNews as sn
from news_data import CCTVNews as cn
from news_data import CswNews as csn
from news_data import ZqrbNews as zn
from news_data import ClsNews as cln
from finance_data import GoodsData as gd
from finance_data import StocksData as sd
from util.DbUtil import SqlUtils
import util.DbUtil as db
import util.SchedulerUtil as su
import datetime

# ================================ 爬取数据并存入数据库 （头） ================================
# 执行次数
count = 0

# 获取数据并且插入数据库
def getData():
    # 初始化数据库
    sql = SqlUtils()

    print("====任务开始====")

    # findFinanceData(sql)  # 获取金融数据
    findNewsData(sql)     # 获取新闻数据

    global count
    count += 1
    print("====第(%d)次任务执行完毕====" % (count))
    print("-------------------------------------------")



# 获取新闻数据
def findNewsData(sql):
    print("====新闻数据获取并入库====")
    # 获取新闻列表
    newsArray = []
    newsArray.extend(sn.getSinaNews())
    newsArray.extend(cn.getCCTVNews())
    newsArray.extend(csn.getCswNews())
    newsArray.extend(zn.getZqrbNews())
    newsArray.extend(cln.getClsNews())
    for item in newsArray:
        print("item == ", item)
        sql.insert(db.TABLE_NEWS, item)



# 获取金融数据
def findFinanceData(sql):
    currentWeek = datetime.datetime.now().weekday()
    if currentWeek > 4 : return  # 如果当天是周六日，则不查询金融数据
    print("====金融数据获取并入库====")
    # 获取大宗商品数据
    goodsData = gd.getGoodsData()
    print(goodsData.__dict__)
    sql.insert(db.TABLE_GOODS, goodsData.__dict__)
    # 获取股市数据
    stockData = sd.getGoodsData()
    sql.insert(db.TABLE_STOCKS, stockData.__dict__)
    print(stockData.__dict__)

# ================================ 爬取数据并存入数据库 （尾） ================================

# ==主程序==
def main():
    print("====开始执行任务====")
    su.doScheduleJob(getData)
    # getData()

if __name__ == '__main__':
    try:
        main()
    except KeyboardInterrupt:
        print('谢谢使用')
        exit(0)